package com.wn.service;

import com.common.base.BaseMapper;
import com.common.base.BaseService;
import com.common.entity.Hotel;
import com.common.entity.PageParam;
import com.common.entity.Rsp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.HotelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class HotelServiceImpl implements BaseService<Hotel> {

    @Autowired
    HotelMapper hotelMapper;

    @Override
    public PageInfo<Hotel> searchSomeByPage(PageParam<Hotel> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Hotel> newscategoryList=hotelMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Hotel> newshotelPageInfo = new PageInfo<Hotel>(newscategoryList);

            return newshotelPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Hotel> newscategoryList=hotelMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Hotel> newshotelPageInfo = new PageInfo<Hotel>(newscategoryList);

            return newshotelPageInfo;

        }
    }

    @Override
    public Hotel getOneById(String id) {

        return null;
    }

    @Override
    public Rsp addOne(Hotel hotel) {
        hotel.setId("hotel-"+UUID.randomUUID().toString().replaceAll("-",""));
        return hotelMapper.addOne(hotel)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    @Override
    public Rsp update(Hotel hotel) {
        return hotelMapper.update(hotel)==1?Rsp.success("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp deleteLogical(Hotel hotel) {
        hotel.setDeleted("1");
        return hotelMapper.update(hotel)==1?Rsp.success("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    public List<Hotel> getHotelAndHotelroomitem(String id) {
        List<Hotel> hotelAndHotelroomitem = hotelMapper.findHotelAndHotelroomitem(id);
        return hotelAndHotelroomitem;
    }

    public List<Hotel> getById(String userid) {
        List<Hotel> byId = hotelMapper.getById(userid);
        return byId;
    }


    public Rsp updatehert(String id, Double grade) {
        return hotelMapper.updatehert(id,grade)==1?Rsp.success("修改成功"):Rsp.fail().msg("修改失败");
    }
}
